package com.dhcc.sds.busi.controller;


import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.sys.cache.config.CdcConfigCache;
import com.dhcc.cdc.sys.entity.ConfigurationEntity;
import com.dhcc.cdc.sys.service.IConfigurationService;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.security.ShiroKit;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.core.modules.system.entity.User;
import com.dhcc.sds.base.cache.qcentity.IQcEntityCache;
import com.dhcc.sds.base.cache.qcentity.QcEntityCache;
import com.dhcc.sds.base.service.ISdsCommonService;
import com.dhcc.sds.busi.entity.SdsQcincaseEntity;
import com.dhcc.sds.busi.entity.SdsQcincaseOperlogEntity;
import com.dhcc.sds.busi.service.ISdsQcincaseService;
import com.dhcc.sds.busi.vo.AnalysisSdsPortalVo;
import com.dhcc.sds.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 病种入组病例
 * 
 * @ClassName: SdsQcincaseController
 * @Description: TODO
 * @author sunyingjie
 * @date 2021-03-22 10:09:15
 */
@Controller
@RequestMapping("${sys.backendPath}/sds/sdsqcincase")
public class SdsQcincaseController extends BaseController {

	private String PREFIX = "/backend/sds/sdsqcincase2/";

	@Autowired
	private ISdsQcincaseService sdsQcincaseService;

    @Autowired
    private IConfigurationService configurationService;
    @Autowired
    private ISdsCommonService sdsCommonService;
	

    /**
     * 跳转到病种入组病例列表首页
     */
    @RequestMapping("")
    public String index(Model model) {

        DateTime lastDay = new DateTime();
        DateTime firstday = DateUtil.getDateByDay(lastDay, -365);

        model.addAttribute("startDate", firstday);
        model.addAttribute("endDate", lastDay);
        // 单病种医生是否可以直接上报配置
        String formCheckType = CdcConfigCache.me().getValueByKey("SDS", "SDFormCheckType");
        // 是否启用过程质控
        String enableIPQC = CdcConfigCache.me().getValueByKey("SDS", "EnableIPQCModule");
        //默认加载所有科室
//        JSONArray departMap = sdsCommonService.getDepartMap(new HashMap<>());
        JSONObject departMap = sdsCommonService.findDeptList();

        model.addAttribute("formCheckType", formCheckType);
        model.addAttribute("enableIPQC", enableIPQC);
        model.addAttribute("departMap", departMap);

        return PREFIX + "sdsqcincase2.html";
    }

    /**
     * 跳转到病种入组病例列表首页
     */
    @RequestMapping("/lite")
    public String lite(Model model) {

        DateTime lastDay = new DateTime();
        DateTime firstday = DateUtil.getDateByDay(lastDay, -365);

        model.addAttribute("startDate", firstday);
        model.addAttribute("endDate", lastDay);
        // 单病种医生是否可以直接上报配置
        String formCheckType = CdcConfigCache.me().getValueByKey("SDS", "SDFormCheckType");
        // 是否启用过程质控
        String enableIPQC = CdcConfigCache.me().getValueByKey("SDS", "EnableIPQCModule");
        model.addAttribute("formCheckType", formCheckType);
        model.addAttribute("enableIPQC", enableIPQC);
        return PREFIX + "sdsqcincase_lite.html";
    }
	
    /**
     * 跳转到新增病种入组病例
     */
    @RequestMapping("/to_add")
    public String toAdd() {
        return PREFIX + "sdsqcincase_add.html";
    }
	
	/**
     * 跳转到修改病种入组病例
     */
    @RequestMapping("/to_update/{sdsQcincaseId}")
    public String toUpdate(@PathVariable Long sdsQcincaseId, Model model) {
        SdsQcincaseEntity sdsQcincase = this.sdsQcincaseService.selectById(sdsQcincaseId);
        model.addAttribute("sdsQcincase",sdsQcincase);
        return PREFIX + "sdsqcincase_edit.html";
    }

    /**
     * 获取病种入组病例列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam Map<String, Object> params) {
		Page<SdsQcincaseEntity> page = new PageFactory<SdsQcincaseEntity>().defaultPage();
        List<SdsQcincaseEntity> result = this.sdsQcincaseService.page(page,params);
        page.setRecords((List<SdsQcincaseEntity>) new EmptyWarpper(result).warp());
        page.setTotal(result.size());
        return R.packForBT(page);
    }

    /**
     * 获取迟报病例列表
     */
    @RequestMapping(value = "/getDelayReport")
    @ResponseBody
    public Object getDelayReport(@RequestParam Map<String, Object> params){
        // 查询登录用户关联科室
        List<String> deptIds = sdsCommonService.getDeptIdsBySysUser();
        params.put("userLinkDepts", deptIds);
        Page<SdsQcincaseEntity> page = new PageFactory<SdsQcincaseEntity>().defaultPage();
        List<SdsQcincaseEntity> result = this.sdsQcincaseService.getDelayReport(page,params);
        page.setRecords((List<SdsQcincaseEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }


    /**
     * 新增病种入组病例
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(SdsQcincaseEntity sdsQcincase) {
        if (CommonUtil.isOneEmpty(sdsQcincase)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        sdsQcincase.insert();
        return R.ok();
    }

    /**
     * 删除病种入组病例
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Map<String, Object> params) {
        Long mrListId = Long.valueOf(params.get("mrListId").toString());
        String execUser;
        if (CommonUtil.isNotEmpty(params.get("execUser"))) {
            execUser = params.get("execUser").toString();
        } else {
            User user = ShiroKit.getUser();
            execUser = user.getLoginName();
        }
	    Date now = new Date();
        this.sdsQcincaseService.deleteStatusById(mrListId);
        // 增加病例操作日志
        SdsQcincaseOperlogEntity sdsQcincaseOperlogEntity = new SdsQcincaseOperlogEntity();
        sdsQcincaseOperlogEntity.setInCaseId(mrListId);
        sdsQcincaseOperlogEntity.setOperState("O");
        sdsQcincaseOperlogEntity.setOperUser(execUser);
        sdsQcincaseOperlogEntity.setOperDate(now);
        sdsQcincaseOperlogEntity.insert();

        return R.ok();
    }

    /**
     * 修改病种入组病例
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(SdsQcincaseEntity sdsQcincase) {
        if (CommonUtil.isOneEmpty(sdsQcincase)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        SdsQcincaseEntity old = this.sdsQcincaseService.selectById(sdsQcincase.getId());

        old.setEpisodeId(sdsQcincase.getEpisodeId());
        old.setEntityId(sdsQcincase.getEntityId());
        old.setEntityVerId(sdsQcincase.getEntityVerId());
        old.setInUserId(sdsQcincase.getInUserId());
        old.setInLocId(sdsQcincase.getInLocId());
        old.setInWardId(sdsQcincase.getInWardId());
        old.setInDate(sdsQcincase.getInDate());
        old.setInTime(sdsQcincase.getInTime());
        old.setOutDocId(sdsQcincase.getOutDocId());
        old.setOutDate(sdsQcincase.getOutDate());
        old.setOutTime(sdsQcincase.getOutTime());
        old.setState(sdsQcincase.getState());
        old.setRelativeCpId(sdsQcincase.getRelativeCpId());
        old.updateAllColumnById();
        return R.ok();
    }

    /**
     * 获取质控病种字典列表(不分页)
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/qryQCEntity")
    @ResponseBody
    public Object qryQCEntity(@RequestParam Map<String, Object> params, String dateType, String startDate, String endDate) {
        List<SdsQcincaseEntity> result = this.sdsQcincaseService.listByDate(params, dateType, startDate, endDate);
        return result;
    }

    /**
     * 获取质控病种字典列表(不分页)
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/qryQCEntity2")
    @ResponseBody
    public Object qryQCEntity2(@RequestParam Map<String, Object> params) {
        return this.sdsQcincaseService.listByDate2(params);
    }

    /**
     * 获取病种入组病例列表
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/list2")
    @ResponseBody
    public Object list2(@RequestParam Map<String, Object> params) {
        Page<SdsQcincaseEntity> page = new PageFactory<SdsQcincaseEntity>().defaultPage();
        List<SdsQcincaseEntity> result = this.sdsQcincaseService.page2(page,params);
        page.setRecords((List<SdsQcincaseEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    @RequestMapping(value = "/getMrListInfo")
    @ResponseBody
    public Object getMrListInfo(@RequestParam Map<String, Object> params){
        return this.sdsQcincaseService.getMrListInfo(params);
    }

    @RequestMapping(value = "/changeMrEntity")
    @ResponseBody
    public Object changeMrEntity(@RequestParam Map<String, Object> params){
        this.sdsQcincaseService.changeMrEntity(params);
        return R.ok();
    }
	
}
